package com.teacher.controller;

import com.teacher.entity.TeacherComments;
import com.teacher.service.TeacherCommentsService;
import com.teacher.untils.ResultEntity;
import com.teacher.untils.ResultUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;
import org.springframework.web.util.WebUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "教师评语管理")
@RequestMapping("/teacherComments")
@RestController
public class TeacherCommentsController {
    @Autowired
    private TeacherCommentsService teacherCommentsService;

    // 批量导入教师评语
    @ApiOperation("教师评语批量导入")
    @RequestMapping(value = "/excelImport/teacherComments", method = RequestMethod.POST)
    public ResultEntity uploadCommentsOfExcel(HttpServletRequest servletRequest){
        MultipartFile file = null;
        if (servletRequest instanceof StandardMultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = WebUtils.getNativeRequest(servletRequest, MultipartHttpServletRequest.class);
            List<MultipartFile> fileList = multipartRequest.getFiles("file");
            if (null != fileList)
                file = fileList.get(0);
        }
        if(file.isEmpty()) return ResultUtils.build("文件不能为空");
        String academicYear = servletRequest.getParameter("academicYear");
        String type = servletRequest.getParameter("type");
        return ResultUtils.build(teacherCommentsService.uploadCommentsOfExcel(file, academicYear, type));
    }

    // 增加
    @ApiOperation("教师评语增加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultEntity  addTeacherComments(@RequestBody TeacherComments teacherComments){
        return ResultUtils.build(teacherCommentsService.addTeacherComments(teacherComments));
    }

    // 根据id查看详情
    @ApiOperation("教师评语查看详情")
    @GetMapping(value = "/getDetailsById/{id}")
    public ResultEntity getDetailsById(@PathVariable("id") String id){
        return ResultUtils.build(teacherCommentsService.getDetailsById(id));
    }

    // 根据id删除
    @ApiOperation("教师评语信息删除")
    @DeleteMapping("/delete/{id}")
    public void deleteTeacherComments(@PathVariable("id") String id){
        teacherCommentsService.deleteTeacherComments(id);
    }

    // 根据id批量删除
    @ApiOperation("教师评语信息批量删除")
    @DeleteMapping("/batch")
    public void deleteTeacherCommentsList(String idList){
        teacherCommentsService.deleteTeacherCommentsList(idList);
    }

    // 根据条件分页查询
    @ApiOperation(value = "根据条件分页查询",notes = "根据条件分页查询")
    @GetMapping("/specificPage")
    public ResultEntity specificPageQueryTeacherCommentsList(TeacherComments teacherComments,int page,int size){
        return ResultUtils.build(teacherCommentsService.specificPageQueryTeacherCommentsList(teacherComments,page,size));
    }
}
